package com.hospital.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @description：
 * @author：zou
 * @date：2021/11/16 9:52
 * @versions：v1.0
 */
@Configuration
public class JobConfig {

    //1.我要做什么（工作）
    //2.什么时候执行（触发器）
    //3.什么时候执行工作（任务调度）

    //1.我要做什么
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean jbf = new JobDetailFactoryBean();
        jbf.setJobClass(CheckStorage.class);
        return jbf;
    }

    //2.什么时候执行
    @Bean
    public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean ctf = new CronTriggerFactoryBean();
        //1.装配任务
        ctf.setJobDetail(jobDetailFactoryBean.getObject());
        //2.设置执行时间
        ctf.setCronExpression("0 0/30 * * * ?");
        return ctf;
    }

    //3.什么时间执行什么事情
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) {
        SchedulerFactoryBean sdf = new SchedulerFactoryBean();
        sdf.setTriggers(cronTriggerFactoryBean.getObject());
        return sdf;
    }

}
